package com.example.myproject.controller;

import com.example.myproject.dto.SignInResponse;
import com.example.myproject.security.JwtAuthenticationFilter;
import com.example.myproject.service.SignInService;
import com.example.myproject.vo.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api/values")
public class SignInController {

    @Autowired
    private SignInService signInService;

    @PostMapping("/signin")
    public ApiResponse<SignInResponse> signIn(
            @AuthenticationPrincipal JwtAuthenticationFilter.UserPrincipal principal) {
        if (principal == null) {
            return ApiResponse.error(401, "请先登录");
        }

        try {
            SignInResponse response = signInService.signIn(principal.getId());
            return ApiResponse.success(200, response.getMessage(), response);
        } catch (RuntimeException e) {
            return ApiResponse.error(400, e.getMessage());
        }
    }
}